package com.woniuxy.jiapei.controller;


import cn.hutool.crypto.digest.DigestUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.woniuxy.jiapei.pojo.Customer;
import com.woniuxy.jiapei.pojo.User;
import com.woniuxy.jiapei.service.CustomerService;
import com.woniuxy.jiapei.util.JwtUtils;
import com.woniuxy.jiapei.util.R;
import com.woniuxy.jiapei.util.ResultCode;
import com.woniuxy.jiapei.vo.LoginVo;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * customer 前端控制器
 * </p>
 *
 * @author lr
 * @since 2020-10-21
 */
@CrossOrigin
@RestController
@RequestMapping("/jiapei/customer")
public class CustomerController {
    @Autowired
    private CustomerService customerService;
    @ApiOperation("测试接口")
    @GetMapping("/test")
    public R test() {
        List<Customer> list = customerService.list();
        System.out.println("hello");
        System.out.println("hello");
        return R.ok().data("list",list);
    }
    @ApiOperation("测试接口")
    @GetMapping("/test2")
    public R test2() {
        List<Customer> list = customerService.list();
        return R.ok().data("list",list);
    }
    @ApiOperation("用户登录接口")
    @PostMapping("/login")
    public R login(@RequestBody LoginVo loginVo) {
        QueryWrapper<Customer> wrapper = new QueryWrapper<>();
        wrapper.eq("mobile",loginVo.getUsername());
        wrapper.eq("password",DigestUtil.md5Hex(loginVo.getPassword()));
        Customer customer = customerService.getOne(wrapper);
        if (customer==null){
            return R.error().code(ResultCode.LOGIN_ERROR).message("登陆失败");
        }
        String token = JwtUtils.createToken(customer.getMobile(), 60 * 60);
        return R.ok().data("token",token).code(ResultCode.LOGIN_OK);
    }
    @ApiOperation("根据token返回用户信息接口")
    @GetMapping("/info")
    public R info(@RequestParam("token") String token) {
        System.out.println(token);
        if (token == null) {
            return R.error().code(ResultCode.LOGIN_ERROR).message("无token信息");
        }
        try {
            String username = JwtUtils.getUsername(token);
            List<String> roles = new ArrayList<>();
            roles.add("普通用户");
            return R.ok().data("name",username)
                    .code(ResultCode.LOGIN_OK)
                    .data("avatar","https://wpimg.wallstcn.com/f778738c-e4f8-4870-b634-56703b4acafe.gif")
                    .data("introduction","正常用户")
                    .data("roles",roles);
        } catch (Exception e) {
            e.printStackTrace();
            return R.error().code(ResultCode.LOGIN_ERROR).message("无效token");
        }
    }
    @ApiOperation("用户退出接口")
    @PostMapping("/logout")
    public R loginout(@RequestHeader("X-Token") String token) {
        System.out.println(token);
        //省略session,redis等业务逻辑
        return R.ok().code(ResultCode.LOGIN_OK).message("登出成功");
    }

    @ApiOperation("用户注册")
    @PostMapping("/register")
    public R register(@RequestBody Customer customer){
        System.out.println(customer);
        customer.setType("0");
        customer.setCreateTime(new Date());
        customer.setStatus(1);
        customer.setPassword(DigestUtil.md5Hex(customer.getPassword()));
        boolean save = customerService.save(customer);
        if (save) {
            return R.ok().message("注册成功");
        }
        return R.error().message("注册失败");
    }
}

